//
//  HomeViewController.swift
//  IDF
//
//  Created by 陈徐挺 on 22/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit
import PromiseKit
import SwiftyJSON
import YYCache
import SVProgressHUD
import BEMCheckBox
import SwifterSwift

class HomeViewController: BaseViewController {

    @IBOutlet weak var label_1: UILabel!
    @IBOutlet weak var label_2: UILabel!
    @IBOutlet weak var label_3: UILabel!
    @IBOutlet weak var label_4: UILabel!
    @IBOutlet weak var label_5: UILabel!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var contentWidth: NSLayoutConstraint!
    
    @IBOutlet weak var agreemenLinkBtn: UIButton!
    
    @IBOutlet weak var borrowBtn: UIButton!
    
    @IBOutlet weak var label_6: UILabel!
    
    
    
    
    
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if appLaunched {
            return
        }
        let launchView = LaunchView(frame: appWindow.bounds)
        appWindow.addSubview(launchView)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        fd_prefersNavigationBarHidden = true
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        fd_prefersNavigationBarHidden = true
        navigationController?.setNavigationBarHidden(true, animated: false)
        let _ = AppConfig.getAppConfig().then{[weak self] obj -> Void in
            appConfig = obj
        }
        getInitDebitStyle()
        contentWidth.constant = (kWidth - 130) / 2
        
        agreemenLinkBtn.setBlockFor(.touchUpInside) {[weak self] _ -> Void in
            let url = URL(string: appConfig.agreementUrl!)
            let next: WebViewController = initViewContollerWithStoryboard(storyboardName: "Mine", storyboardId: "webViewController")
            next.urlRequest = URLRequest(url: url!)
            self?.navigationController?.pushViewController(next, animated: true)
        }
        let x = ( kWidth - 163 ) / 2 - 30
        let checkBox = BEMCheckBox(frame: CGRect(x: x, y: 315, width: 20, height: 20))
        checkBox.boxType = .square
        checkBox.onAnimationType = .fill
        checkBox.offAnimationType = .fill
        checkBox.lineWidth = 2
        checkBox.onFillColor = UIColor(hexString: "8cadf5")!
        checkBox.onTintColor = UIColor(hexString: "8cadf5")!
        checkBox.onCheckColor = .white
        checkBox.delegate = self
        
        scrollView.addSubview(checkBox)
        
        borrowBtn.alpha = 0.4
        borrowBtn.isEnabled = false
        
        var htmlText = "<span style=\"font-family: PingFang SC; font-size:13px;color: #000; text-decoration: underline;\">I agree the loan agreement</span>"
        label_6.attributedText = htmlText.html2AttributedString
        
    }
    @IBAction func borrowNow(_ sender: Any) {
        if appLogined {
            let next: LoanInformationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "loanInformationViewController")
            nowNavgationController.pushViewController(next, animated: true)
        } else {
            showLoginView()
        }
    }
    func getInitDebitStyle() {
        DebitStyle.getDebitStyle().then{[weak self] obj -> Void in
            debitStyleModel = obj
            self?.label_1.text = "Loan Amount for \(debitStyleModel.debitPeriod!) days"
            self?.label_2.text = "Rp\(Int(debitStyleModel.debitMoney!))"
            self?.label_3.text = "Payback After \(debitStyleModel.debitPeriod!) days"
            self?.label_4.text = "Rp\(Int(debitStyleModel.payBackMoney!))"
            self?.label_5.text = "Admin Fee Rp \(debitStyleModel.debitFee!)"
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension HomeViewController: BEMCheckBoxDelegate {
    func didTap(_ checkBox: BEMCheckBox) {
        dPrint(checkBox.on)
        if checkBox.on {
            borrowBtn.alpha = 1
            borrowBtn.isEnabled = true
        } else {
            borrowBtn.alpha = 0.4
            borrowBtn.isEnabled = false
        }
    }
}
